angular.
module('adminuserChange').
component('adminuserChange', {
  templateUrl:
  'views/app/adminuser-change/adminuser-change.template.html',

  controller: function ($scope,$cookies,$http,$window,$location,$rootScope,$routeParams,$compile) {
        $scope.myRights = readCookie('right')
        if ($scope.myRights != undefined){
          if ($scope.myRights.indexOf('admin') == -1){
                      swal("没有操作权限", '请联系管理员!', "error").then(function(){
            })
          return false
        }
        
        }        else{
          $window.location = '/login'
        }


          $scope.shadow = false
          $scope.myrights = []
          $scope.passx = 0
          $scope.rights = [{'name':'创建/管理 管理员','value':'admin'},{'name':'添加学生','value':'students'},{'name':'添加课程/教材','value':'courseAndProduct'},{'name':'校区管理','value':'school'},
          {'name':'排版','value':'Scheduling'},{'name':'财务','value':'finance'},{'name':'员工管理','value':'staff'}]
          $scope.id = $routeParams.id

          var condition = {}
          // get info
          if ($scope.id != undefined){
          $http({
              method: 'post',
              url: '/admin/adminuserGet',
              data : {'session':$cookies.get('sessionID'),'account':$scope.id}
              
          }).success(function(datax) {
            $scope.account = datax.data.userid
            $scope.password = datax.data.password
            $scope.passx = 1
            $scope.myrights = datax.data.rights
            
          })         
          } 


        $scope.changepass = function(){
          $scope.passx = 0
        }


      $scope.newAdmin = function(){
       var message = ''

          if ($scope.account.length < 3){
            message += '管理员账号长度不能小于3位  \n'
          }
          if ($scope.password.length < 6){
            message += '管理员密码不能小于6位  \n'
          }      
          if ($scope.myrights.length == 0){
            message += '管理员权限不能为空  \n'
          }
          if (message.length != 0){
            swal("信息不全!", message, "error").then(function(){
            })
            return false
          }
          else{
            $scope.shadow = true
            var url = ''
            if ($routeParams.id == undefined){
              url = '/admin/addAdminUser'
            }
            else{
              url = '/admin/editAdmin'
            }
            
            var data = {}
            data.account = $scope.account
            data.password = $scope.password
            data.rights = $scope.myrights
            data.passx = $scope.passx
            
            data.token = $cookies.get('sessionID')
            // swal.showLoading()
            $http({
              method : 'post',
              url : url,
              data : data}).success(function(data){
                if (data.code == 401){
                  $window.location = '/login'
                  return false
                }
                else if (data.code == 402){
                  $scope.shadow = false
                      swal("该管理员已经存在" ,'', "error").then(function(){

                                   }) 
                      return false
                }

               else if (data.code == 200 ){
                  
                  $scope.shadow = false
                  swal("修改成功!" , '',"success").then(function(){
                    // to template list
                    $window.location = 'adminuser_list'
                  })            
                
              }
              else{
                $scope.shadow = false
                                   swal("修改失败!" , '',"error").then(function(){

                                   })    
              }
             
            })
          }
        }



      function formatDate(date){
        var xdate = new Date(date)
        var year = xdate.getFullYear()
        var day = xdate.getDate()
        var month = xdate.getMonth()
        var hour = xdate.getHours()
        var minute = xdate.getMinutes()
        return String(year) + '/' + String(month+1) + '/' + String(day) + ' ' + String(addZeroBefore(hour)) + ':' + String(addZeroBefore(minute))
      }



      function addZeroBefore(n) {
        return (n < 10 ? '0' : '') + n;
      }




        }})



